<template>
	<view class='bb'>
		<swiper class="detail-1" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">

			<swiper-item class="detail-item" v-for="(item,index) in goodsInfo.pics" :key='index'>
				<image class="detail-img" :src="item.pics_big" mode="" @click="bigtu(index)"></image>
			</swiper-item>
		</swiper>
		<view class="detail-2" v-if="goodsInfo.goods_name">
			<view class="price">￥{{goodsInfo.goods_price}}</view>
			<view class="detail-ok">
				<view class="detali-left">
					{{goodsInfo.goods_name}}
				</view>
				<view class="detail-right">
					<uni-icons type="star" size="30" color="red"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<view class="yunfei">快递：免运费</view>
		</view>
		<rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
		<view class="nav">
			<uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="iconclick"
				@buttonClick="buttonClick"></uni-goods-nav>
		</view>
	</view>
</template>

<script>
	import {
		goodsdetaildata
	} from '@/api/goods_list_request.js';
	import {
		mapMutations,
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				goodsInfo: {},
				options: [{
						icon: 'shop',
						text: '商店'

					},
					{
						icon: 'cart',
						text: '购物车',
						info: 2
					}
				],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: "立即购买",
						backgroundColor: 'orange',
						color: '#fff'
					}
				]

			};
		},
		computed: {
			...mapGetters('cart_m', ['total'])
		},
		watch:{
			total:{
				handler(newValue){
					console.log(newValue);
					const findResult = this.options.find(item => item.text == '购物车');
					if (findResult) {
						findResult.info = newValue
					}
				},
				immediate:true
			}
		},
		onLoad(options) {
			console.log(options);
			this.goodsdetail(options.goods_id)

		},
		methods: {
			...mapMutations('cart_m', ['addTo']),
			async goodsdetail(goods_id) {
				const {
					data: res
				} = await goodsdetaildata(goods_id);
				console.log(res);
				if (res.meta.status !== 200) return uni.$showMsg();
				res.message.goods_introduce = res.message.goods_introduce.replace(/<img/g,
					'<img style="display:block;"').replace(/webp/g, 'jpg');
				this.goodsInfo = res.message;
			},
			bigtu(index) {
				console.log(111)
				uni.previewImage({
					current: index,
					// urls: this.goodsInfo.pics.map(item => this.pics_big)
					urls: this.goodsInfo.pics.map(item => this.pics_big)
				})
			},
			iconclick(e) {
				console.log('左侧');
				console.log(e);
				if (e.content.text === '购物车') {
					uni.switchTab({
						url: '../../pages/cart/cart'
					})
				}
			},
			buttonClick(e) {
				console.log('右侧');
				console.log(e);
				if (e.content.text === '加入购物车') {
					const goods = {
						goods_id: this.goodsInfo.goods_id,
						goods_name: this.goodsInfo.goods_name,
						goods_price: this.goodsInfo.goods_price,
						goods_count: 1,
						goods_small_logo: this.goodsInfo.goods_small_logo,
						goods_state: true
					}
					this.addTo(goods);
					// const findResult = this.options.find(item => item.text == '购物车');
					// if (findResult) {
					// 	findResult.info = this.total
					// }
				}
			}

		}
	}
</script>

<style lang="scss">
	.bb {
		padding-bottom: 50px;

		.detail-1 {
			height: 300px;

			.detail-item {
				.detail-img {
					width: 100%;
					height: 100%;
				}
			}
		}

		.detail-2 {
			padding: 10px;

			.price {
				color: red;
				font-size: 20px;
			}

			.detail-ok {
				display: flex;

				.detali-left {
					font-size: 15px;
					padding-right: 10px;
				}

				.detali-right {
					width: 100px;
					font-size: 15px;
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;
					border-left: 1px solid red;

				}
			}

			.yunfei {
				color: gray;
				font-size: 12px;
				margin: 10px 0;
			}
		}

		.nav {
			position: fixed;
			left: 0;
			bottom: 0;
			width: 100%;
		}
	}
</style>
